package cn.e4j.modular.api.service;


import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.modular.api.mapper.BfcglMapper;
import cn.e4j.modular.api.mapper.BftjMapper;
import cn.e4j.modular.api.util.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
public class BftjService {


    @Autowired
    BftjMapper bftjMapper;

    @Transactional(readOnly = true)
    public List<Dto> getList(Dto indto) {
        List<Dto> ps = bftjMapper.getPerson(indto);
        LocalDate now = LocalDate.now();
        for (Dto p : ps) {
            p.put("todayHours", 0);

            //当日
            Dto query = new Dto();
            query.put("type", 1);
            query.put("now", now);
            query.put("GWBM001", p.getString("GWBM001"));
            query.put("ENT", indto.getString("ENT"));
            List<Dto> list = InvokeUtil.invokeBeanMethod2("cn.e4j.modular.api.mapper.BftjMapper.getList", query);


            p.put("todayCounts", ToolUtil.isEmpty(list) ? 0 : list.size());
            if (p.getInteger("todayCounts") > 0) {
                long count = 0;//计算时长
                for (Dto d : list) {
                    if (ToolUtil.isNotEmpty(d.getString("GWBM007"))) {
                        LocalDate gwbm003 = CommonUtils.dateToLocalDate(d.getDate("GWBM003"));
                        LocalDateTime reachTime = LocalDateTime.of(gwbm003, LocalTime.parse(d.getString("GWBM004")));
                        LocalDateTime leaveTime = LocalDateTime.of(gwbm003, LocalTime.parse(d.getString("GWBM007")));
                        count += Duration.between(reachTime, leaveTime).toHours();
                    }
                }
                p.put("todayHours", count);
            }

            //本期
            p.put("hours", 0);
            query = new Dto();
            query.put("type", 2);
            query.put("now", now);
            query.put("GWBM001", p.getString("GWBM001"));
            query.put("ENT", indto.getString("ENT"));
            query.put("startDate", indto.getString("startDate"));
            query.put("endDate", indto.getString("endDate"));
            //list = bftjMapper.getList(query);
            list = InvokeUtil.invokeBeanMethod2("cn.e4j.modular.api.mapper.BftjMapper.getList", query);
            p.put("counts", ToolUtil.isEmpty(list) ? 0 : list.size());
            if (p.getInteger("counts") > 0) {
                long count = 0;//计算时长
                for (Dto d : list) {
                    if (ToolUtil.isNotEmpty(d.getString("GWBM007"))) {

                        LocalDate gwbm003 = CommonUtils.dateToLocalDate(d.getDate("GWBM003"));
                        LocalDateTime reachTime = LocalDateTime.of(gwbm003, LocalTime.parse(d.getString("GWBM004")));

                        LocalDateTime leaveTime = LocalDateTime.of(gwbm003, LocalTime.parse(d.getString("GWBM007")));
                        count += Duration.between(reachTime, leaveTime).toHours();
                    }
                }
                p.put("hours", count);
            }
        }
//        ps=new ArrayList<>();
//        Dto dto = new Dto();
//        dto.put("NAME","小菜1");
//        dto.put("hours","20");
//        dto.put("counts","6");
//        dto.put("todayHours","2");
//        dto.put("todayCounts","1");
//        ps.add(dto);
//
//        dto = new Dto();
//        dto.put("NAME","小菜2");
//        dto.put("hours","15");
//        dto.put("counts","5");
//        dto.put("todayHours","4");
//        dto.put("todayCounts","2");
//        ps.add(dto);
//
//        dto = new Dto();
//        dto.put("NAME","小菜3");
//        dto.put("hours","30");
//        dto.put("counts","10");
//        dto.put("todayHours","3");
//        dto.put("todayCounts","1");
//        ps.add(dto);

        return ps;
    }

    @Transactional(readOnly = true)
    public List<Dto> getInfo(Dto indto) {
        List<Dto> infos = InvokeUtil.invokeBeanMethod2("cn.e4j.modular.api.mapper.BftjMapper.getList", indto);
        List<Dto> result = new ArrayList<>();
        for (Dto d : infos) {
            if (ToolUtil.isNotEmpty(d.getString("GWBM007")) && ToolUtil.isNotEmpty(d.getString("GWBM004"))) {
                LocalDate gwbm003 = CommonUtils.dateToLocalDate(d.getDate("GWBM003"));
                LocalDateTime reachTime = LocalDateTime.of(gwbm003, LocalTime.parse(d.getString("GWBM004")));
                LocalDateTime leaveTime = LocalDateTime.of(gwbm003, LocalTime.parse(d.getString("GWBM007")));
                d.put("hours", Duration.between(reachTime, leaveTime).toHours());
            }else
                d.put("hours", "-");

            result.add(d);
        }

        return result;
    }
}
